<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/6/8
 * Time: 14:58
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\web\controller;

use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\MachineCate;
use app\common\model\MachineMachine;
use app\common\model\MallOrder;
use app\common\model\MallOrderGoods;
use Htlove\Time;

class Order extends Base
{
    /**
     * 获取订单信息
     * @return array|\think\response\Json
     * @throws \think\Exception
     */
    public function getOrderByInfo()
    {
        if (!$this->request->isPost()) {
            return Show::json_show(Status::mini_error, "接口错误");
        }
        $time = $this->request->param('time', '', 'trim');
        $page = $this->request->param('page', 1, 'intval');
        $limit = $this->request->param('limit', 10, 'intval');
        if ($this->s_id == 0) {
            return Show::json_show(Status::mini_noLogin, "请先登录");
        }
        $where = [];
        if (empty($time)) {
            $where[] = [
                ['create_time', 'between', Time::getInstance()->month()]
            ];
        } else {
            $where[] = [
                ['create_time', 'between', [
                    strtotime($time[0]),
                    strtotime($time[1]),
                ]]
            ];
        }
        if($this->s_cate != 0){
            $where[] = [
                ['cate_id','=',$this->s_cate]
            ];
        }
        $lists = (new MallOrder())->getPaginateByNormalListInfo($where, $limit, 'id,order_sn,pay_price,create_time', 'id desc');
        return Show::json_show(Status::mini_normal, "ok", $lists['data'], $lists['total']);
    }

    /**
     * 获取今日 昨日 本月 今年销售统计
     * @return array|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderByMoney()
    {
        $obj = new MallOrder();
        $where = [];
        if($this->s_cate != 0){
            $where[] = [
                ['cate_id','=',$this->s_cate]
            ];
        }
        $today = $obj->getOrderByOrderFind(array_merge([
            ['create_time', 'between', Time::getInstance()->today()]
        ],$where), 'sum(pay_price) as money');
        $yesterday = $obj->getOrderByOrderFind(array_merge([
            ['create_time', 'between', Time::getInstance()->yesterday()]
        ],$where), 'sum(pay_price) as money');
        $month = $obj->getOrderByOrderFind(array_merge([
            ['create_time', 'between', Time::getInstance()->month()]
        ],$where), 'sum(pay_price) as money');
        $year = $obj->getOrderByOrderFind(array_merge([
            ['create_time', 'between', Time::getInstance()->year()]
        ],$where), 'sum(pay_price) as money');
        return Show::json_show(Status::mini_normal, 'ok', [
            "today" => $today['money'] ?? 0,
            "yesterday" => $yesterday['money'] ?? 0,
            "month" => $month['money'] ?? 0,
            "year" => $year['money'] ?? 0,
        ]);
    }

    public function getTopOrderByInfo()
    {
        $type = $this->request->param('type', 0, 'intval');
        $where = [];
        if ($type == 0) {
            $where[] = [
                ['o.create_time', 'between', Time::getInstance()->today()]
            ];
        } else if ($type == 1) {
            $where[] = [
                ['o.create_time', 'between', Time::getInstance()->yesterday()]
            ];
        } else if ($type == 2) {
            $where[] = [
                ['o.create_time', 'between', Time::getInstance()->month()]
            ];
        } else if($type == 3) {
            $time = $this->request->param('time', '', 'trim');
            $where[] = [
                ['o.create_time', 'between', [
                    strtotime($time." 00:00:00"),
                    strtotime($time." 23:59:59"),
                ]]
            ];
        }
        if($this->s_cate != 0){
            $where[] = [
                ['r.cate_id','=',$this->s_cate]
            ];
        }

        $lists = (new MallOrderGoods())
            ->field('o.specs_name,o.specs_title,r.pay_price,count(o.id) as count')
            ->alias('o')
            ->join('mall_order r','o.order_id = r.id')
            ->where('r.order_status',1)
            ->where($where)
            ->group('specs_name')
            ->order('count desc')
            ->select()
            ->toArray();
        return Show::json_show(Status::mini_normal,'ok',$lists);

    }
}